package mini.ideashare.cms.controller;

import com.alibaba.fastjson.JSON;
import com.taobao.api.ApiException;
import com.taobao.api.TaobaoClient;
import com.taobao.api.domain.NTbkItem;
import com.taobao.api.request.TbkItemGetRequest;
import com.taobao.api.response.TbkItemGetResponse;
import mini.ideashare.cms.base.BaseResponse;
import mini.ideashare.cms.base.PageData;
import mini.ideashare.cms.common.TBClientFactory;
import mini.ideashare.cms.manager.TB_ItemManager;
import mini.ideashare.cms.model.TB_Item;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class TB_ItemController extends AbstractBaseController {

    @Autowired
    private TB_ItemManager tbItemManager;

    @GetMapping("/item/query")
    @ResponseBody
    public BaseResponse<PageData<NTbkItem>> queryItem(TbkItemGetRequest item){

        TaobaoClient client = TBClientFactory.getClient();

        item.setFields("num_iid,title,pict_url,small_images,reserve_price,zk_final_price,user_type,provcity,item_url,seller_id,volume,nick");
        TbkItemGetResponse rsp = null;
        try {
            rsp = client.execute(item);
        } catch (ApiException e) {
            e.printStackTrace();
        }

        int pageNo = item.getPageNo()==null?0:item.getPageNo().intValue();
        int pageSize = item.getPageSize()==null?0:item.getPageSize().intValue();

        return  BaseResponse.assemblePageResponse(rsp.getResults(),rsp.getTotalResults(),pageNo,pageSize);

    }

    @PostMapping("/item/save")
    @ResponseBody
    public BaseResponse<String> saveItem(TB_Item item){
        BaseResponse<String> baseResponse = new BaseResponse<>();
        tbItemManager.saveItem(item);
        baseResponse.setData("OK");
        return  baseResponse;
    }

    @GetMapping("/item/list")
    @ResponseBody
    public String listItem(TB_Item item, Integer pageNumber , Integer pageSize){

        Map<String,Object> map  = new HashMap<>();

        List<TB_Item> tb_items = tbItemManager.queryItem(item, pageNumber, pageSize);
        Long totalCount = tbItemManager.countItem(item);
        map.put("total",totalCount);
        map.put("rows",tb_items);
        return JSON.toJSONString(map);
    }

    @GetMapping("/item/showDetail")
    public String showDetail(Long id, Model model){

        TB_Item item = new TB_Item();
        item.setId(id);
        List<TB_Item> tb_items = tbItemManager.queryItem(item, 1, 1);

        model.addAttribute("item",tb_items.get(0));
        return "item/detail";
    }
//    @PostMapping("/item/importExcel")
//    @ResponseBody
//    public List<TB_Item> importExcel(MultipartFile excelFile){
//
//        List<TB_Item> failList = new ArrayList<>();
//        try {
//            InputStream in = excelFile.getInputStream();
//            List<TB_Item> tb_items = IS_ExcelUtils.excelToListForXLS(in, TB_Item.class);
//
//
//            for (TB_Item tb_item : tb_items) {
//                try {
//                    int i = tbItemManager.saveItem(tb_item);
//                    if(i<1){
//                        failList.add(tb_item);
//                    }
//                } catch (Exception e) {
//                    failList.add(tb_item);
//                    e.printStackTrace();
//                }
//            }
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//        return failList;
//    }

    @GetMapping("/item/delete")
    @ResponseBody
    public String delete(Long id){
        tbItemManager.deleteItem(id);
        return "OK";
    }
}
